﻿//https://leetcode.cn/problems/jJ0w9p/

class Solution {
public:
    int mySqrt(int x)
    {
        if (x == 0)
            return 0;
        //袖珍计算器算法
        // int ans=exp(0.5*log(x));
        // return ((long long)(ans+1)*(ans+1)<=x?ans+1:ans);

        //二分查找
        int left = 0, right = x, ans = -1;
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            if ((long long)mid * mid <= x)
            {
                ans = mid;
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }
        }
        return ans;
    }
};